<template>
    <Row>
        <i-col span="8">
            <Select style="width:150px" placeholder="请选择区县" :disabled="countyDisable" v-model="countyValue" clearable
                    @on-change="countySelect">
                <Option v-for="item in countyList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
        </i-col>
        <i-col span="8">
            <Select style="width:150px" placeholder="请选择乡镇/街道" :disabled="streetDisable" v-model="streetValue" clearable
                    @on-change="streetSelect">
                <Option v-for="item in streetList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
        </i-col>
        <i-col span="8">
            <Select style="width:150px" placeholder="请选择社区/村" :disabled="communityDisable" v-model="communityValue"
                    clearable @on-change="communitySelect">
                <Option v-for="item in communityList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
        </i-col>
    </Row>
</template>

<script>
    import {postAction} from '@/api/request'

    export default {
        name: "area-linkage",
        props: {
            areaCode: {
                type: String,
                default: ""
            },
            countyDisable: {
                type: Boolean,
                default: false
            },
            streetDisable: {
                type: Boolean,
                default: false
            },
            communityDisable: {
                type: Boolean,
                default: false
            },
            time: {
                type: String,
                default: ""
            }

        },
        data() {
            return {
                countyValue: "",
                streetValue: "",
                communityValue: "",
                countyList: [],
                streetList: [],
                communityList: [],
                areaList: [],
            }
        },
        methods: {
            //重置数据
            reset() {
                this.streetList.length = 0;
                this.streetValue = "";
                this.communityList.length = 0;
                this.communityValue = "";
            },
            //加载乡镇/街道信息
            loadStreet(areaCode) {
                var streetList = this.areaList.jddata;
                for (var i in streetList) {
                    if (streetList[i].parentId == areaCode.substring(0, 6) + "000000") {
                        this.streetList.push(streetList[i]);
                    }
                }
            },
            //加载社区/村信息
            loadCommunity(areaCode) {
                var communityList = this.areaList.sqdata;
                for (var i in communityList) {
                    if (communityList[i].parentId == areaCode.substring(0, 9) + "000")
                        this.communityList.push(communityList[i]);
                }
            },

            //选择区县
            countySelect(e) {
                this.$emit('update:areaCode', e);
            },
            //选择街道
            streetSelect(e) {
                if (!e) {
                    e = this.countyValue;
                }
                this.$emit('update:areaCode', e);
            },
            //选择社区/村
            communitySelect(e) {
                if (!e) {
                    e = this.streetValue;
                    if (!e) {
                        e = this.countyValue;
                    }
                }
                this.$emit('update:areaCode', e);
            },
            //加载数据
            loadInformation(areaCode) {
                if (areaCode.length == 12) {
                    if (areaCode.substring(6, 12) == "000000") {
                        this.loadStreet(areaCode);
                        this.countyValue = areaCode;
                    } else if (areaCode.substring(9, 12) == "000") {
                        this.loadStreet(areaCode);
                        this.loadCommunity(areaCode);
                        this.countyValue = areaCode.substring(0, 6) + "000000";
                        this.streetValue = areaCode;
                    } else {
                        this.loadStreet(areaCode);
                        this.loadCommunity(areaCode);
                        this.countyValue = areaCode.substring(0, 6) + "000000";
                        this.streetValue = areaCode.substring(0, 9) + "000";
                        this.communityValue = areaCode;
                    }
                }
            },
            loadArea() {
                var that = this;
                postAction('/api/ac/subsistence/ss01001Service/getEffectiveAreaCode', {time: this.time}).then((res) => {
                    that.areaList = res.data.data;
                    that.countyList = res.data.data.xqdata;
                    if (that.areaCode) {
                        that.loadInformation(that.areaCode);
                    }
                })
            }
        },
        watch: {
            time: function () {
                this.loadArea();
            },
            areaCode: function () {
                this.reset();
                if (this.areaCode) {
                    this.loadInformation(this.areaCode)
                } else {
                    this.countyValue = "";
                }
            }
        },
        created() {
            this.loadArea();
        }
    }
</script>

<style scoped>

</style>